<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>pvspitch</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="pvsosc.html" title="pvsosc" />
    <link rel="next" href="pvstanal.html" title="pvstanal" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">pvspitch</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="pvsosc.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="pvstanal.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="pvspitch"></a>
      <div class="titlepage"></div>
      <a id="Indexpvspitch" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">pvspitch</span>
        </h2>
        <p>pvspitch — 
      Track the pitch and amplitude of a PVS signal.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp139716176"></a>
        <h2>Description</h2>
        <p>
      Track the pitch and amplitude of a PVS signal as k-rate variables.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp139717568"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">kfr, kamp <span class="command"><strong>pvspitch</strong></span> fsig, kthresh</pre>
      </div>
      <div class="refsect1">
        <a id="idp139719808"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>kamp</em></span> -- Amplitude of fundamental frequency
    </p>
        <p>
      <span class="emphasis"><em>kfr</em></span> -- Fundamental frequency
    </p>
        <p>
      <span class="emphasis"><em>fsig</em></span> -- an input pv stream
    </p>
        <p>
      <span class="emphasis"><em>kthresh</em></span> -- analysis threshold (between 0 and 1). Higher values will eliminate low-amplitude components from the analysis.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp139724304"></a>
        <h2>Performance</h2>
        <p>
      The pitch detection algorithm implemented by <span class="emphasis"><em>pvspitch</em></span> is based upon  J. F. Schouten's hypothesis of the neural processes of the brain used to determine the pitch of a sound after the frequency analysis of the basilar membrane.  Except for some further considerations, <span class="emphasis"><em>pvspitch</em></span> essentially seeks out the highest common factor of an incoming sound's spectral peaks to find the pitch that may be attributed to it.
    </p>
        <p>
      In general, input sounds that exhibit pitch will also exhibit peaks in their spectrum according to where their harmonics lie. There are some the exceptions, however.  Some sounds whose spectral representation is continuous can impart a sensation of pitch.  Such sounds are explained by the centroid or center of gravity of the spectrum and are beyond the scope of the method of pitch detection implemented by <span class="emphasis"><em>pvspitch</em></span> (Using opcodes like <a class="link" href="pvscent.html" title="pvscent"><em class="citetitle">pvscent</em></a> might be more appriopriate in these cases).
    </p>
        <p>
      <span class="emphasis"><em>pvspitch</em></span> is able (using a previous analysis <span class="emphasis"><em>fsig</em></span> generated by <a class="link" href="pvsanal.html" title="pvsanal"><em class="citetitle">pvsanal</em></a>) to locate the spectral peaks of a signal. The threshold parameter (<span class="emphasis"><em>kthresh</em></span>) is of utmost importance, as adjusting it can introduce weak yet significant harmonics into the calculation of the fundamental. However, bringing <span class="emphasis"><em>kthresh</em></span> too low would allow harmonically unrelated partials into the analysis algorithm and this will compromise the method's accuracy.  These initial steps emulate the response of the basilar membrane by identifying physical characteristics of the input sound. The choice of <span class="emphasis"><em>kthresh</em></span> depends on the actual level of the input signal, since its range (from 0 to 1) spans the whole dynamic range of an analysis bin (from -inf to 0dBFS).
    </p>
        <p>
      It is important to remember that the input to the <span class="emphasis"><em>pvspitch</em></span> opcode is assumed to be characterised by strong partials within its spectrum.  If this is not the case, the results outputted by the opcode may not bear any relation to the pitch of the input signal.  If a spectral frame with many unrelated partials was analysed, the greatest common factor of these frequency values that allows for adjacent “harmonics” would be chosen.  Thus, noisy frames can be characterised by low frequency outputs of <span class="emphasis"><em>pvspitch</em></span>. This fact allows for a primitive type of instrumental transient detection, as the attack portion of some instrumental tones contain inharmonic components.  Should the lowest frequency of the analysed melody be known, then all frequencies detected below this threshold are inaccurate readings, due to the presence of unrelated partials.
    </p>
        <p>
      In order to facilitate efficient testing of the <span class="emphasis"><em>pvspitch</em></span> algorithm, an amplitude value proportional to the one in the observed in the signal frame is also outputted (<span class="emphasis"><em>kamp</em></span>).  The results of <span class="emphasis"><em>pvspitch</em></span> can then be employed to drive an oscillator whose pitch can be audibly compared with that of the original signal (In the example below, an oscillator generates a signal which appears a fifth above the detected pitch).
    </p>
      </div>
      <div class="refsect1">
        <a id="idp139738704"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the pvspitch opcode. It uses the file <a class="ulink" href="examples/pvspitch.csd" target="_top"><em class="citetitle">pvspitch.csd</em></a>. This example uses realtime audio input but can be used for audiofile input as well.
      </p>
        <div class="example">
          <a id="idp139740736"></a>
          <p class="title">
            <strong>Example 710. Example of the pvspitch opcode</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o pvspitch.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="ohdr">sr</span> <span class="op">=</span> 44100
<span class="ohdr">ksmps</span> <span class="op">=</span> 128
<span class="ohdr">nchnls</span> <span class="op">=</span> 1


giwave <span class="ohdr">ftgen</span> 0, 0, 4096, 10, 1, 0.5, 0.333, 0.25, 0.2, 0.1666 

<span class="oblock">instr</span> 1

ifftsize <span class="op">=</span> 1024
iwtype <span class="op">=</span> 1    <span class="op">/</span><span class="op">*</span> cleaner with hanning window <span class="op">*</span><span class="op">/</span>

a1 <span class="opc">inch</span> 1 <span class="comment">;Realtime audio input</span>
<span class="comment">;a1   soundin "input.wav" ;Use this line for file input</span>

fsig <span class="opc">pvsanal</span>   a1, ifftsize, ifftsize<span class="op">/</span>4, ifftsize, iwtype
kfr, kamp <span class="opc">pvspitch</span>   fsig, 0.01

adm  <span class="opc">oscil</span>     kamp, kfr <span class="op">*</span> 1.5, giwave  <span class="comment">;Generate note a fifth above detected pitch</span>

       <span class="opc">out</span>    adm
<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

<span class="stamnt">i</span> 1 0 30

<span class="stamnt">e</span>

<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
      </div>
      <div class="refsect1">
        <a id="idp139745024"></a>
        <h2>See Also</h2>
        <p>
      <a class="link" href="pvsanal.html" title="pvsanal"><em class="citetitle">pvsanal</em></a>,
      <a class="link" href="pvsynth.html" title="pvsynth"><em class="citetitle">pvsynth</em></a>,
      <a class="link" href="pvsadsyn.html" title="pvsadsyn"><em class="citetitle">pvsadsyn</em></a>,
      <a class="link" href="pvscent.html" title="pvscent"><em class="citetitle">pvscent</em></a>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp139750016"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Alan OCinneide</td>
          </tr>
          <tr>
            <td>August 2005, added by Victor Lazzarini, August 2006</td>
          </tr>
          <tr>
            <td>Part of the text has been adapted from the Csound Journal winter 2006 issue's article "Introducing PVSPITCH: A pitch tracking opcode for Csound" by Alan OCinneide. The article is available at: <a class="ulink" href="http://www.csoundjournal.com/2006winter/pvspitch.html" target="_top"><em class="citetitle">www.csoundjournal.com/2006winter/pvspitch.html</em></a></td>
          </tr>
        </table>
        <p>
    </p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="pvsosc.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="pvstanal.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">pvsosc </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> pvstanal</td>
        </tr>
      </table>
    </div>
  </body>
</html>
